import { createRouter, createWebHistory } from 'vue-router'
import { routes } from 'vue-router/auto-routes'
import { loginInfo } from '@/store/loginInfo'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    ...routes, // 自动导入路由
    {
      path: '/t',
      component: () => import('@/pages/login.vue')
    }
  ]
})

router.beforeEach(async (to, from) => {
  if (
    // 检查用户是否已登录
    !loginInfo.isLogin &&
    // ❗️ 避免无限重定向
    to.name !== '/login'
  ) {
    // 将用户重定向到登录页面
    return { name: '/login' }
  }
})

export default router